package com.wyait.manage.web.work;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyait.manage.entity.Internship;
import com.wyait.manage.entity.UserRoleDTO;
import com.wyait.manage.entity.UserSearchDTO;
import com.wyait.manage.entity.Work;
import com.wyait.manage.pojo.User;
import com.wyait.manage.service.InternshipService;
import com.wyait.manage.service.WorkService;
import com.wyait.manage.service.impl.WorkComputeService;
import com.wyait.manage.utils.ExcelServiceUtil;
import com.wyait.manage.utils.PageDataResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Work)表控制层
 *
 * @author makejava
 * @since 2020-05-14 15:28:16
 */
@Controller
@RequestMapping("/work")
@Slf4j
public class WorkController {
    /**
     * 服务对象
     */
    @Resource
    private WorkService workService;

    @Autowired
    private InternshipService internshipService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Work selectOne(Integer id) {
        return this.workService.queryById(id);
    }



    @RequestMapping("/workList")
    public String toWorkList() {
        return "/work/workList";
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value={"workManage","workView"},logical=Logical.OR)
    public PageDataResult getUsers(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit, UserSearchDTO userSearch) {
        log.debug("分页查询工作列表！搜索条件：userSearch：" + userSearch + ",page:" + page
                + ",每页记录数量limit:" + limit);
        PageDataResult pdr = new PageDataResult();
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        try {
            if (null == page) {
                page = 1;
            }
            if (null == limit) {
                limit = 10;
            }
            // 获取用户和角色列表pdr =
            String workId = user.getWorkId();
            System.out.println(workId);
            List<Work> all = null;
            if("".equals(workId) || workId == null){
                all = workService.getAll();
            } else {
                all = workService.queryByWorkId(workId);
            }

            PageHelper.startPage(page, limit);
            // 获取分页查询后的数据
            PageInfo<Work> pageInfo = new PageInfo<>(all);

            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            pdr.setList(all);
            log.debug("工作列表列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("工作列表查询异常！", e);
        }
        return pdr;
    }


}